package com.situ.meetingRoom.dao.impl;

import com.situ.meetingRoom.dao.UserDao;
import com.situ.meetingRoom.entity.User;
import com.situ.meetingRoom.util.DBUtil;
import com.situ.meetingRoom.util.PageInfo;
import com.situ.meetingRoom.vo.UserVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserDaoImpl implements UserDao {

    private DBUtil dbUtil = new DBUtil();

    @Override
    public int insert(User user) {
        String sql = "insert into user (password,name,sex,tel,email,role,dept_id) values (?,?,?,?,?,?,?)";
        return dbUtil.update(sql, user.getPassword(), user.getName(), user.getSex(), user.getTel(), user.getEmail(), user.getRole(), user.getDeptId());
    }

    @Override
    public int update(User user) {
        String sql = "update user " +
                "set ";
        List<Object> params = new ArrayList<>();
        if (user.getPassword() != null && user.getPassword().length() > 0) {
            sql += "password=?,";
            params.add(user.getPassword());
        }
        if (user.getName() != null && user.getName().length() > 0) {
            sql += "name=?,";
            params.add(user.getName());
        }
        if (user.getSex() != null && user.getSex().length() > 0) {
            sql += "sex=?,";
            params.add(user.getSex());
        }
        if (user.getTel() != null && user.getTel().length() > 0) {
            sql += "tel=?,";
            params.add(user.getTel());
        }
        if (user.getEmail() != null && user.getEmail().length() > 0) {
            sql += "email=?,";
            params.add(user.getEmail());
        }
        if (user.getHead() != null && user.getHead().length() > 0) {
            sql += "head=?,";
            params.add(user.getHead());
        }
        if (user.getRole() != null) {
            sql += "role=?,";
            params.add(user.getRole());
        }
        if (user.getStatus() != null) {
            sql += "status=?,";
            params.add(user.getStatus());
        }
        if (user.getDeptId() != null) {
            sql += "dept_id=?,";
            params.add(user.getDeptId());
        }
        sql = sql.substring(0, sql.length() - 1);
        sql += " where id = ?";
        params.add(user.getId());
        return dbUtil.update(sql, params.toArray());
    }

    @Override
    public List<UserVO> selectAll(User user) {
        String sql = "select user.* , dept.name deptName from user inner join dept on user.dept_id = dept.id where 1=1 ";
        List<Object> params = new ArrayList<>();
        if (user.getName() != null && user.getName().length() > 0) {
            sql += "and user.name like ? ";
            params.add("%" + user.getName() + "%");
        }

        if (user.getSex() != null && user.getSex().length() > 0) {
            sql += "and sex = ? ";
            params.add(user.getSex());
        }

        if (user.getRole() != null) {
            sql += "and role = ? ";
            params.add(user.getRole());
        }

        if (user.getStatus() != null) {
            sql += "and user.status = ? ";
            params.add(user.getStatus());
        }

        if (user.getDeptId() != null) {
            sql += "and dept_id = ? ";
            params.add(user.getDeptId());
        }

        return dbUtil.query(UserVO.class, sql, params.toArray());
    }

    @Override
    public User selectById(Integer id) {
        String sql = "select * from user where id=?";
        return dbUtil.queryOne(User.class, sql, id);
    }

    @Override
    public User selectByName(String name) {
        String sql = "select * from user where name = ?";
        return dbUtil.queryOne(User.class, sql, name);
    }

    @Override
    public User selectTel(String tel) {
        String sql = "select * from user where tel = ?";
        return dbUtil.queryOne(User.class, sql, tel);
    }

    @Override
    public PageInfo selectForPage(Integer page, Integer limit, User user) {
        String sqlWhere = "";
        List<Object> params = new ArrayList<>();

        if (user.getName() != null && user.getName().length() > 0) {
            sqlWhere += " and user.name like ? ";
            params.add("%" + user.getName() + "%");
        }

        if (user.getSex() != null && user.getSex().length() > 0) {
            sqlWhere += "and sex = ? ";
            params.add(user.getSex());
        }

        if (user.getRole() != null) {
            sqlWhere += "and role = ? ";
            params.add(user.getRole());
        }

        if (user.getStatus() != null) {
            sqlWhere += "and user.status = ? ";
            params.add(user.getStatus());
        }

        if (user.getDeptId() != null) {
            sqlWhere += "and dept_id = ? ";
            params.add(user.getDeptId());
        }

        //记录的条数
        String sql = "select count(*) count from user where 1=1 " + sqlWhere;
        List<Map<String, Object>> list1 = dbUtil.query(sql, params.toArray());
        Long count = (Long) list1.get(0).get("count");


        sql = "select user.* , dept.name deptName from user inner join dept on user.dept_id = dept.id where 1=1  " + sqlWhere +
                " limit ?,? ";

        params.add((page - 1) * limit);
        params.add(limit);

        List list = dbUtil.query(sql, params.toArray());

        PageInfo pageInfo = new PageInfo(count, list);

        return pageInfo;
    }

    @Override
    public List<User> selectDeptId(Integer id) {
        String sql = "select * from user where status = 0 and dept_id = ?";
        return dbUtil.query(User.class, sql, id);
    }


}
